package interfaces.hud.pilot.status;

import networking.messages.syncMessages.ValueType;

import org.fenggui.util.Color;

import logic.common.player.Player;
import logic.common.player.playerListeners.HunterChangedEvent;
import logic.common.player.playerListeners.KillAssistEvent;
import logic.common.player.playerListeners.KillEvent;
import logic.common.player.playerListeners.MissionAssistEvent;
import logic.common.player.playerListeners.PlayerListener;
import logic.common.player.playerListeners.RespawnEvent;
import logic.common.player.playerListeners.TargetChangedEvent;
import logic.ships.hunter.CounterMeasure;
import logic.ships.hunter.Hunter;
import logic.status.StatusChangedEvent;
import logic.status.StatusListener;
import fileHandling.language.interfaces.HUDText;
import interfaces.GUISource;
import interfaces.PropertyContent;
import interfaces.hud.BasicHUD;
import interfaces.hud.spectate.SpectateChangeEvent;
import interfaces.hud.spectate.SpectateListener;
import interfaces.hud.spectate.SpectateWidget;

public class CounterMeasureHUD extends PropertyContent implements SpectateWidget {
	
	private Hunter hunter;
	private StatusListener listener;
	private BasicHUD hud;
	
	public CounterMeasureHUD(int width, int height, int x, int y, final Player player, BasicHUD hud) {
		super(width, height, x, y, HUDText.Counter_Measures, Mode.UP);
		this.hud = hud;
		
		GUISource.setFont(descriptionLabel, GUISource.smallFont);
		
		setMaxValue(CounterMeasure.RELOAD_TIME);
		setValue(CounterMeasure.RELOAD_TIME);
		
		changeBackgroundColor(Color.WHITE_HALF_TRANSPARENT);
		changeTextColor(Color.WHITE);
		changeValueColor(Color.BLUE);
		
		listener = new StatusListener() {
			@Override
			public void valueChanged(StatusChangedEvent event) {
				ValueType type = event.getType();
				switch(type) {
					case CounterMeasures: setValue(CounterMeasure.RELOAD_TIME - event.getFloatValue());
				}
			}
		};
		
		hud.addListener(player, new PlayerListener() {
			@Override
			public void respawned(RespawnEvent event) {
				setHunter(player.getHunter());
			}

			@Override
			public void hunterChanged(HunterChangedEvent event) {}
			@Override
			public void targetChanged(TargetChangedEvent event) {}
			@Override
			public void killed(KillEvent event) {}
			@Override
			public void killAssist(KillAssistEvent event) {}
			@Override
			public void missionAssist(MissionAssistEvent event) {}
		});
		
		if(player.getHunter() != null) setHunter(player.getHunter());
	}
	
	public void setHunter(Hunter hunter) {
		if(this.hunter != null) removeListener();
		this.hunter = hunter;
		if(this.hunter != null) addListener();
	}
	
	private void addListener() {
		assert(hunter != null);
		hud.addListener(hunter, listener);
		setValue(CounterMeasure.RELOAD_TIME - hunter.getCounterMeasureLoad());
	}
	
	private void removeListener() {
		assert(hunter != null);
		hunter.removeStatusListener(listener);
	}
	
	@Override
	public SpectateListener getSpectateListener() {
		return new SpectateListener() {
			@Override
			public void spectating(SpectateChangeEvent event) {
				setHunter(event.getWatchedPlayer().getHunter());
			}
		};
	}
}